<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['params']=array();
$this->model('member')->checklogin();
if($_RQ['op']=='main'){
	$merchid=intval($_RQ['id'])?:intval($_RQ['merchid']);
	$uid=$_RQ['uid']?intval($_RQ['uid']):$_SESSION['uid'];
	if($uid){
		$member = pdo_get('core_members',array('id'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('id','realname','credit2','mobile','groupid','avatar','nickname'));
	}
	if($_RQ['myshop'] || $merchid || $uid){
		$install = check_account_permission('jy_merch');
		if($install){
			if($merchid){
				$merch=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
				if(empty($merch)){
					exi('没有找到该小店！','error','goback');
				}
			}else{
				$merch=pdo_get('jy_merch_user',array('mid'=>$uid));
				if(empty($merch)){
					exi('您还未注册店铺','error','jy_merch/register/');
					/*$data =array(
						'applytime'=>time(),
						'logo'=>$member['avatar'],
						'mobile'=>$member['mobile'],
						'realname'=>$member['realname'],
						'merchname'=>$member['nickname'],
						'desc'=>'欢迎光临：我的小店《'.$member['nickname'].'小店》，我们将竭诚为您服务,祝您购物愉快！',
						'status'=>1,
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$uid,
						'jointime'=>time(),
					);
					pdo_insert('jy_merch_user',$data);
					$merch=pdo_get('jy_merch_user',array('mid'=>$uid));*/
				}elseif($merch['status']!=1){
					exi('申请正在审核请耐心等待','error','jy_merch/register/');
				}else{
					$merchid = $merch['id'];
				}
			}
		}else{
			exi('请联系管理员开启<多商户>！','error');
		}
	}
	if($merch['mid'] == $_SESSION['uid']){
		$return['isme']=1;
	}
	$member = pdo_get('core_members',array('id'=>$merch['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','realname','credit2','mobile','groupid','avatar','nickname'));
	$this->session('merchid',$merchid);
	$this->session('merchmid',$merch['mid']);
	$this->session('copy_merchid',0);
	$merch['islike']=pdo_getcolumn('jy_merch_like_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$merchid),'islike');
	//更新足迹
	$this->model('merch')->up($merchid);
	$merch['logo']=$merch['logo']?tomedia($merch['logo']):$member['avatar'];
	$image=ROOT_AT.'images/jy_weishop/'.$_SESSION['uniacid'].'/merchid'.$merchid.'.jpg';
	if(!file_exists($image)){
		$url='/jy_weishop/merch/index?merchid='.$merchid.'&fromuser='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'images/jy_weishop/'.$_SESSION['uniacid'];
		if(!file_exists($path)){
				mkdir($path,0700, true);
		}
		file_put_contents($path.'/merchid'.$merchid.'.jpg',$res);
	}
	$merch['appcode'] = 'images/jy_weishop/'.$_SESSION['uniacid'].'/merchid'.$merchid.'.jpg';
	$merch['title1']=cfc('tools')->substr($merch['merchname'],0,13);
	$merch['title2']=cfc('tools')->substr($merch['desc'],0,13);
	$merch['title2']=$merch['title2']?$merch['title2'].'...':'暂无描述';
	$return['goodsnum']=pdo_count('jy_weishop_goods',array('merchid'=>$merchid,'status'=>1));
	$return['ordernum']=pdo_count('jy_weishop_order',array('merchid'=>$merchid));
	$merch['salenum']=pdo_getsum('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'total');
	$return['merch']=$merch;
	$return['sharedata']=array(
		'title'=>$merch['merchname'],
		'desc'=>$merch['desc']?$merch['desc']:'欢迎光临：我的小店《'.$merch['merchname'].'小店》，我们将竭诚为您服务,祝您购物愉快！',
		'link'=>custom_url('jy_weishop/merch//id='.$merchid,true),
		'imgUrl'=>$merch['logo'],
	);
	
	$kefuset=$this->plugin_setting('kefu'.$merchid);
	$return['kefu']=$kefuset['open'];
	set_html('title',$merch['title']);
	exi($return);
}
if($_RQ['op']=='list'){
	$merchid =$this->session('merchid');
	$copy_merchid=$this->session('copy_merchid');
	if($copy_merchid){
		$merchid=$copy_merchid;
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'nochecked'=>0,'merchid'=>$merchid);
	if(!$_RQ['setgoods']){
		$params['status']=1;
	}
	
	if($_RQ['keyword']){		
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];	
	}
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
	}
	if($_RQ['order']=='isdiscount'){
		$params['isdiscount']=intval($_RQ['isdiscount']);
	}
	if($_RQ['order']=='issendfree'){
		$params['issendfree']=intval($_RQ['issendfree']);
	}
	if($_RQ['order']=='istime'){
		$params['istime']=intval($_RQ['istime']);
	}
	//按点赞数排序作品
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
	
	$commission_goods=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),'commission_goods');
	$commission_goods=json_decode($commission_goods,true);
	if(!empty($commission_goods)){
		//$commission_goods=array_keys($commission_goods);
		unset($params['merchid']);
		$params['id']=$commission_goods;
		//按点赞数排序作品
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$commlist=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','salenum desc, id desc');
		if(!empty($commlist)){
			$list=array_merge($list,$commlist);
		}
	}
	if(!$list){
		exi('none');
	}
	if($list){
		foreach($list as &$itemv){
			$itemv['createtime']=date('Y-m-d H:i:s',$itemv['createtime']);
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
		}
	}
	exi($list);
}
if($_RQ['op']=='addgoods'){
	$merchid =$this->session('merchid');
	$id=intval($_RQ['id']);
	$item=array('images'=>array(),'type'=>'1');
	if($id){
		$item=pdo_get('jy_weishop_goods',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'id'=>$id));	
		if(empty($item)){
			exi('此商品不是你的，不能编辑！','error','goback');
		}
		$item['images']=json_decode($item['images'],true);
		$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
		
		if($specs_list){
			foreach($specs_list as &$row){
				$row['options']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$row['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
			}
			$item['specs']['specs']=$specs_list;
		}
		$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
		if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$item['specs']['tds']=$option_list;
		}
	}
	$postdata=get_postdata();
	if($postdata){
		$id=$this->session('id');
		$data =array(
			'title'=>trim($postdata['title']),
			'image'=>trim($postdata['image']),
			'subtitle'=>trim($postdata['subtitle']),
			'price'=>$postdata['price'],
			'unit'=>$postdata['unit']?trim($postdata['unit']):'件',
			'status'=>1,
			'type'=>$postdata['type'],
			'weight'=>$postdata['weight'],
			'total'=>intval($postdata['total']),
			'pcate'=>intval($postdata['pcate']),
			'images'=>ijson_encode($postdata['images']),
			
		);
		if($id){
			pdo_update('jy_weishop_goods',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['merchid']=$merchid;
			$data['total']=$postdata['total']?intval($postdata['total']):8;
			$jy_merch=plugin('jy_merch')->plugin_setting('setting');
			if($jy_merch['isshenhe']){
				$data['nochecked']=1;
			}else{
				$data['nochecked']=0;
			}			
			pdo_insert('jy_weishop_goods',$data);
		}
		$goodsid=intval($postdata['id']);
		//整理商品规格数据
		if($postdata['specs']){
			$spec_ids=array();//接收当前生效的规格ID
			$spec_option_ids=array();//接收当前生效的规格项ID
			$spec_goods_ids=array();//接收当前生效的规格商品ID
			//第一步，整理规格和规格项
			foreach($postdata['specs']['specs'] as $spec_key=>&$spec){
				if(!$spec['id']){
					
					//数据库里面没有该规格
					$new_spec=array(
						'uniacid'=>$_SESSION['uniacid'],
						'goodsid'=>$goodsid,
						'paixu'=>$spec_key,
						'title'=>$spec['title'],
					);
					pdo_insert('jy_weishop_goods_spec',$new_spec);
					$spec['id']=pdo_insertid();
				}else{
					pdo_update('jy_weishop_goods_spec',array('title'=>$spec['title']),array('id'=>$spec['id']));
				}
				$spec_ids[]=$spec['id'];
				foreach($spec['options'] as $option_key=>&$option){
					if(!$option['id']){
						$new_spec_option=array(
							'uniacid'=>$_SESSION['uniacid'],
							'goodsid'=>$goodsid,
							'specid'=>$spec['id'],
							'title'=>$option['title'],
							'paixu'=>$option_key,
						);
						pdo_insert('jy_weishop_goods_spec_item',$new_spec_option);
						$option['id']=pdo_insertid();
					}else{
						pdo_update('jy_weishop_goods_spec_item',array('title'=>$option['title']),array('id'=>$option['id']));
					}
					$spec_option_ids[]=$option['id'];
				}
			}
			
			
			//第二步，整理规格商品数据
			foreach($postdata['specs']['tds'] as $key=>$spec_goods){
				$data=array(
					'paixu'=>$key,
					'thumb'=>trim($spec_goods['image']),
					'stock'=>intval($spec_goods['num']),
					'price'=>$spec_goods['price'],
					'productprice'=>$spec_goods['old_price'],
					'costprice'=>$spec_goods['cost_price'],
					'goodssn'=>trim($spec_goods['bm']),
					'productsn'=>trim($spec_goods['tm']),
					'weight'=>round($spec_goods['weight'],2),
				);
				if($spec_goods['id']){
					pdo_update('jy_weishop_goods_option',$data,array('id'=>intval($spec_goods['id']),'uniacid'=>$_SESSION['uniacid']));
				}else{
					//新的规格商品，根据规格值找到相应规格ID
					$data['specs']=array();
					foreach($postdata['specs']['specs'] as $sp){
						foreach($sp['options'] as $op){
							if($spec_goods[$sp['title']]==$op['title']){
								$data['specs'][]=$op['id'];
								break;
							}
						}
					}
					$data['specs']=implode('_',$data['specs']);
					$data['uniacid']=$_SESSION['uniacid'];
					$data['goodsid']=$goodsid;
					pdo_insert('jy_weishop_goods_option',$data);
					$spec_goods['id']=pdo_insertid();
				}
				$spec_goods_ids[]=$spec_goods['id'];
			}
			//清理数据库中弃用的规格、规格项、规格商品
			if(!empty($spec_ids)){
				pdo_delete('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_ids));
			}
			if(!empty($spec_option_ids)){
				pdo_delete('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_option_ids));
			}
			if(!empty($spec_goods_ids)){
				pdo_delete('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid,'id !='=>$spec_goods_ids));
			}
			$options = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price asc');
			$options2 = pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsid),'price','','price desc');
			if($options){
				$upgoods =array();
				$upgoods['minprice'] = $options[0]['price'];
				$upgoods['maxprice'] = $options2[0]['price'];
				pdo_update('jy_weishop_goods',$upgoods,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		exi('保存成功','','/merch/main/myshop=1');
	}else{
		$this->session('id',$id);
	}
	$return['item']=$item;
	$return['types']=$this->goods_types;
	exi($return);
}
if($_RQ['op']=='set'){
	$merchid =$this->session('merchid');
	$item=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));	
	$item['lat_lng']=json_decode($item['lat_lng'],true);
	$item['is_share']=$item['is_share']?true:false;
	$item['iscommission']=$item['iscommission']?true:false;
	$item['iskefu']=$item['iskefu']?true:false;
	$postdata=get_postdata();
	if($postdata){
		$data =array(
			'merchname'=>trim($postdata['merchname']),
			'topimg'=>trim($postdata['topimg']),
			'salecate'=>trim($postdata['salecate']),
			'desc'=>$postdata['desc'],
			'logo'=>$postdata['logo'],
			'realname'=>trim($postdata['realname']),
			'mobile'=>trim($postdata['mobile']),
			'is_share'=>$postdata['is_share']?1:0,
			'iscommission'=>$postdata['iscommission']?1:0,
			'iskefu'=>$postdata['iskefu']?1:0,
			'lat_lng'=>ijson_encode($postdata['lat_lng']),
			'address'=>trim($postdata['address']),
		);
		pdo_update('jy_merch_user',$data,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		//添加客服
		$install = check_account_permission('jy_kefu');
		if($install){
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar','mobile','groupid'));
			$kefuinfo=array(
				'nickname'=>trim($member['nickname']),
				'number'=>$postdata['mobile']?trim($postdata['mobile']):trim($member['mobile']),
				'picurl'=>trim($member['avatar']),
				'plugin'=>trim($member['plugin']),
				'pid'=>intval($merchid),
				'mid'=>$_SESSION['uid'],
			);
			$kefuinfo['type']=0;
			$kefuinfo['name']='kefu'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
			$kefuinfo['type']=1;
			$kefuinfo['name']='kefu1_'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
			$kefuinfo['type']=2;
			$kefuinfo['name']='kefu2_'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
		}else{
			//exi('无法开启客服，联系管理员开启<全网客服>','error');
		}
		exi('设置成功','','/merch/main/myshop=1');
	}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='setgoods'){
	$copy_merchid=intval($_RQ['merchid']);
	$return['sale']=intval($_RQ['sale']);
	if($copy_merchid){
				$merch=pdo_get('jy_merch_user',array('mid'=>$_SESSION['uid']));
				$commission_goods=json_decode($merch['commission_goods'],true);
				if(empty($merch)){
					$data =array(
						'applytime'=>time(),
						'mobile'=>$member['mobile'],
						'realname'=>$member['realname'],
						'merchname'=>$member['nickname'],
						'desc'=>'欢迎光临：我的小店《'.$member['nickname'].'小店》，我们将竭诚为您服务,祝您购物愉快！',
						'status'=>1,
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid']
					);
					pdo_insert('jy_merch_user',$data);
					$merch=pdo_get('jy_merch_user',array('mid'=>$_SESSION['uid']));
				}
				$merchid = $merch['id'];
				$this->session('merchmid',$_SESSION['uid']);
				$this->session('merchid',$merchid);
				$this->session('copy_merchid',$copy_merchid);
	}
	$merchmid =$this->session('merchmid');
	if($merchmid !=$_SESSION['uid']){
		exi('只能设置自己的店铺商品！','error','goback');
	}
	$return['isme']=1;
	$return['params']=array();
	$return['params']['pid']=0;
	$return['copy_merchid']=$copy_merchid;
	$return['list']=array();
	$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isshow'=>1,'parentid'=>0));
	if(empty($category)){
		$category=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0));
	}
	foreach($category as $k=>&$item){
		$goodsnum=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'pcate'=>$item['id']),'id');
		if(empty($goodsnum)){
			unset($category[$k]);
		}else{
			$item['childs']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>1,'parentid'=>$item['id']),array(),'id','paixu desc');
		}
	}
	$return['category']=$category;
	$return['catlevel']=2;
	$return['opencategory']=true;
	set_html('title','我的商品');
	exi($return);
}
if($_RQ['op']=='setstatus'){
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	if(!$id){
		exi('没有商品ID','error');
	}
	$status = pdo_getcolumn('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'status');
	$status = $status?0:1;
	pdo_update('jy_weishop_goods',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('设置成功');
}
if($_RQ['op']=='delgoods'){
	$merchid =$this->session('merchid');
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	$goodsmid=pdo_getcolumn('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'merchid');
	if($id && $goodsmid==$merchid){
		pdo_delete('jy_weishop_goods',array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
		exi('删除成功！');
	}elseif($goodsmid!=$merchid){
		$commission_goods=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'commission_goods');
		$commission_goods=json_decode($commission_goods,true);
		if(!empty($commission_goods)){
			foreach($commission_goods as $k=>$v){
				if($v==$id){
					unset($commission_goods[$k]);
				}
			}
		}
		$commission_goods=ijson_encode($commission_goods);
		pdo_update('jy_merch_user',array('commission_goods'=>$commission_goods),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		exi('删除代销商品成功！');
	}else{
		exi('请选择商品！');
	}
	
}
if($_RQ['op']=='copy_merch'){
	$merchid =$this->session('merchid');
	if($merchid == $_SESSION['uid']){
		exi('不能复制自己的店铺','error');
	}
	$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid),array('merchname','topimg','salecate','desc','logo'));	
	if($merch){
		$data =array(
			'topimg'=>trim($merch['topimg']),
			'salecate'=>trim($merch['salecate']),
			'desc'=>$merch['desc'],
			'logo'=>$merch['logo'],
		);
		pdo_update('jy_merch_user',$data,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	/*$mymerchid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));	
	if($goods){
		foreach($goods as $k=>&$g){
			unset($g['id'],$g['salenum'],$g['salerealnum']);
			$g['merchid']=$mymerchid;
			$g['mid']=$_SESSION['uid'];
			pdo_insert('jy_weishop_goods',$g);
		}
	}*/
	exi('复制成功！','','merch/');
}
if($_RQ['op']=='up_goods'){
	$copy_merchid =$this->session('copy_merchid');
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	if(!$id){
		exi('请选择商品！','error');
	}
	$mymerchid=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'id');
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$copy_merchid,'id'=>$id));	
	unset($goods['id'],$goods['salenum'],$goods['salerealnum']);
	$goods['merchid']=$mymerchid;
	$goods['mid']=$_SESSION['uid'];
	pdo_insert('jy_weishop_goods',$goods);
	exi('复制成功！');
}
if($_RQ['op']=='sale_goods'){
	$copy_merchid =$this->session('copy_merchid');
	$postdata=get_postdata();
	$id = intval($postdata['id']);
	if(!$id){
		exi('请选择商品！','error');
	}
	$commission_goods=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'commission_goods');
	$commission_goods=json_decode($commission_goods,true);
	if(!empty($commission_goods)){
		if(in_array($id,$commission_goods)){
			exi('您已添加该商品！','error');
		}else{
			$commission_goods[]=$id;
		}
	}else{
		$commission_goods[]=$id;
	}
	
	$commission_goods=ijson_encode($commission_goods);
	pdo_update('jy_merch_user',array('commission_goods'=>$commission_goods),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	exi('添加成功！');
}
?>